/* $Id: UserServiceSecurityDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.dao.UserServiceSecurityDAO;
import ae.pcfc.etks.online.common.domain.ServiceRole;
import ae.pcfc.etks.online.common.domain.UserServiceSecurity;

@Repository
public class UserServiceSecurityDAOImpl  extends BaseDAO implements UserServiceSecurityDAO {

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserServiceSecurityDAO#add(ae.pcfc.etks.online.common.domain.UserServiceSecurity)
	 * 
	 */
	public void add(UserServiceSecurity userServiceSecurity) {

		getHibernateTemplate().getSessionFactory().getCurrentSession().save(userServiceSecurity);

	}
	/**
	 * delete
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserServiceSecurityDAO#delete(ae.pcfc.etks.online.common.domain.UserServiceSecurity)
	 * 
	 */
	public void delete(UserServiceSecurity userServiceSecurity) {
		getHibernateTemplate().getSessionFactory().getCurrentSession().delete(userServiceSecurity);
	}
	
	/**
	 * findByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserServiceSecurityDAO#findByUserName(java.lang.String)
	 * 
	 */
	public List<UserServiceSecurity> findByUserName(String username){
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(UserServiceSecurity.class,"uss");
		criteria.add(Restrictions.eq("usSecurityUsername", username).ignoreCase());
		criteria.createAlias("uss.serviceRole", "serviceRole");
		criteria.createAlias("serviceRole.service", "service");
		criteria.add(Restrictions.eq("service.serviceStatus", "A"));
		
		List<UserServiceSecurity> lUserServiceSecurity = criteria.list();
		
		return (lUserServiceSecurity != null && lUserServiceSecurity.size() > 0 )? lUserServiceSecurity : null;
	}
	/**
	 * findByUserNameWithoutDefault
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserServiceSecurityDAO#findByUserNameWithoutDefault(java.lang.String, java.lang.String[])
	 * 
	 */
	public List<UserServiceSecurity> findByUserNameWithoutDefault(String username, String[] arrDefaults){
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(UserServiceSecurity.class,"uss");
		criteria.add(Restrictions.eq("usSecurityUsername", username));
		criteria.createAlias("uss.serviceRole", "serviceRole");
		criteria.createAlias("serviceRole.service", "service");
		criteria.add(Restrictions.eq("service.serviceStatus", "A"));
		if(arrDefaults != null){
			for(String strDefault : arrDefaults){
				//criteria.add(Restrictions.sqlRestriction("nvl(service.serviceDefault,'A') != '" + strDefault + "'"));
				criteria.add(Restrictions.or(Restrictions.isNull("service.serviceDefault"),Restrictions.ne("service.serviceDefault",strDefault)));
			}
		}
		
		List<UserServiceSecurity> lUserServiceSecurity = criteria.list();
		
		return (lUserServiceSecurity != null && lUserServiceSecurity.size() > 0 )? lUserServiceSecurity : null;
	}
	/**
	 * findUserServiceSecurity
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserServiceSecurityDAO#findUserServiceSecurity(ae.pcfc.etks.online.common.domain.UserServiceSecurity)
	 * 
	 */
	public List<UserServiceSecurity> findUserServiceSecurity(UserServiceSecurity userServiceSecurity) {
		// TODO Auto-generated method stub
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(UserServiceSecurity.class,"uss");
		if(userServiceSecurity != null){
			if(userServiceSecurity.getServiceRole() != null){
				ServiceRole serviceRole = userServiceSecurity.getServiceRole();
				criteria.createAlias("uss.serviceRole", "serviceRole");
				criteria.add(Restrictions.eq("serviceRole.srId", serviceRole.getSrId()));
			}
			criteria.add(Restrictions.eq("uss.usSecurityUsername", userServiceSecurity.getUsSecurityUsername()).ignoreCase());
			criteria.add(Restrictions.eq("uss.usSecurityUserType", userServiceSecurity.getUsSecurityUserType()).ignoreCase());
			List<UserServiceSecurity> lUserServiceSecurity = criteria.list();
			
			return (lUserServiceSecurity != null && lUserServiceSecurity.size() > 0 )? lUserServiceSecurity : null;
		}
		return null;
	}
	
}
